package org.farms.algo;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.farms.algo.helpers.Pair;


public class TempMain {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap = new HashMap<Pair<Integer, Integer>, StrategyPoint>();
		Pair<Integer, Integer> searcher = new Pair<Integer, Integer>(0, 0);
		
		int totalGameScore = 0;
		
		Integer[][] defenderPositions = new Integer[100][100];
		for(int i = 0; i < 100; i++) {
			for(int j = 0; j < 100; j++) {
				defenderPositions[i][j] = 0;
			}
		}

		int maxScore = 0;
		int defenderSquads = scan.nextInt();
		scan.nextLine();
		for (int i = 0; i < defenderSquads; i++) {
			String line = scan.nextLine();
			String[] arrLine = line.split(" ");
			if (arrLine.length == 3) {
				fortifyMine(strategyMap, searcher,
						Integer.parseInt(arrLine[1]),
						Integer.parseInt(arrLine[2]));
				defenderPositions[Integer.parseInt(arrLine[1])][Integer.parseInt(arrLine[2])] = -1;
				maxScore += 6;
			} else {
				fortifyChicken(strategyMap, searcher,
						Integer.parseInt(arrLine[1]),
						Integer.parseInt(arrLine[2]),
						Integer.parseInt(arrLine[3]));
				defenderPositions[Integer.parseInt(arrLine[2])][Integer.parseInt(arrLine[3])] = Integer.parseInt(arrLine[1]);
				maxScore += Integer.parseInt(arrLine[1]);
			}
		}
		
		scan.close();
		/*
		Boolean[] flags  = new Boolean[strategyMap.size()];
		//>>>>>>>> Find a way to write the code below better :)
		for(int i = 0; i < strategyMap.size(); i++) {
			flags[i] = true;
		}
		//<<<<<<<<
		*/
		ArrayList<StrategyPoint> strategyList = new ArrayList<StrategyPoint>();
		sortStrategyMap (strategyMap, strategyList);
		
		//Angel Code here >>>>>>>>>
		int[] bombScore = new int[1];
		int remainingMoney = solveBomb(strategyList, searcher, strategyMap, defenderPositions, bombScore); //To define parameters etc.
		int currentScore = bombScore[0];
		//<<<<<
		
		totalGameScore = solvePigs(strategyList, searcher, strategyMap, defenderPositions, remainingMoney, currentScore, maxScore);
		
		//reduceStrategyPoints(strategyMap, searcher, strategyList, flags);
		
		System.out.println("My work here is done!");
		System.out.print("The result is: " + totalGameScore);
	}
	
	private static int solveBomb(ArrayList<StrategyPoint> strategyList,
			Pair<Integer, Integer> searcher, HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap,
			Integer[][] defenderPositions, int[] bombScore) {
		
		//Angel to provide code for!.>>>>>>>
		int[] bombResults = MaxBomb.getBestBomb(defenderPositions);

		int circleX = bombResults[0];
		int circleY = bombResults[1];
		int radius = bombResults[2];
		//<<<<<<<<<<<
		
		for (int i = -radius; i <= radius; i++) {
			for (int j = -radius; j <= radius; j++) {
				if ((i*i) + (j*j) <= (radius*radius)) {
					searcher.setFirst(circleX + i);
					searcher.setSecond(circleY + j);
					
					if (defenderPositions[circleX + i][circleY + j] > 0) {
						destroyChickensAt(defenderPositions[circleX + i][circleY + j],
								circleX + i, circleY + j, strategyList, searcher, strategyMap);
						bombScore[0] += defenderPositions[circleX + i][circleY + j];
						defenderPositions[circleX + i][circleY + j] = 0;
					}
					if (defenderPositions[circleX + i][circleY + j] < 0) {
						destroyMineAt(circleX + i, circleY + j, strategyList, searcher, strategyMap);
						bombScore[0] += 6;
						defenderPositions[circleX + i][circleY + j] = 0;
					}
				}
			}
		}
		searcher.setFirst(circleX);
		searcher.setSecond(circleY);
		if (strategyMap.containsKey(searcher)) {
			strategyList.remove(strategyList.indexOf(strategyMap.get(searcher)));
		}
		
		System.out.println("bomb " + radius + " " + circleX + " " + circleY);
		return 200 - 10*(radius + 1);
	}
	
	private static int solvePigs(ArrayList<StrategyPoint> strategyList, 
			Pair<Integer, Integer> searcher, HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap,
			Integer[][] defenderPositions, int remainingMoney, int currentGameScore, int maxScore) {
		
		int totalGameScore = currentGameScore;
		int isPlayable = 0;
		int positionX = 0;
		int positionY = 0;
		//for(int i = 0; i < strategyList.size() && totalGameScore < maxScore; i++) {
		while (!strategyList.isEmpty() && totalGameScore < maxScore) {
				positionX = strategyList.get(0).getX();
				positionY = strategyList.get(0).getY();
				isPlayable = isPayable(remainingMoney, strategyList.get(0).getCost());
				if (isPlayable > 0 && strategyList.get(0).getProfit() > 0) {
					totalGameScore += strategyList.get(0).getProfit();
					remainingMoney -= isPlayable;
					System.out.println("pigs " + strategyList.get(0).getPigNumber() + " " + positionX + " " + positionY);
				
					for(int a = -1; a < 2 && totalGameScore < maxScore; a++) {
						for (int b = -1; b < 2 && totalGameScore < maxScore; b++) {
							if((positionX + a) >= 0 && (positionX + a) <= 100 
									&& (positionY + b) >= 0 && (positionY + b) <= 100
									&& Math.abs(a) + Math.abs(b) <= 1) {
							
								if (defenderPositions[positionX + a][positionY + b] < 0) {
									destroyMineAt(positionX + a, positionY + b, strategyList, searcher, strategyMap);
									defenderPositions[positionX + a][positionY + b] = 0;
								} 
								if (defenderPositions[positionX + a][positionY + b] > 0) {
									destroyChickensAt(defenderPositions[positionX + a][positionY + b],
											positionX + a, positionY + b, strategyList, searcher, strategyMap);
									defenderPositions[positionX + a][positionY + b] = 0;
								}
							}
						}
					}
					strategyList.remove(0);
					Collections.sort(strategyList, new StrategyPointComparator());
				} else {
					strategyList.remove(0);
				}
		}
		return totalGameScore;
	}
	
	private static void destroyChickensAt (Integer chickenNumbers, int X, int Y,
			ArrayList<StrategyPoint> strategyList, Pair<Integer, Integer> searcher, 
			HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap) {
		
			StrategyPoint pointXY;
			for (int i = -2; i < 3; i++) {
				for( int j = -2; j < 3; j++) {
					if (X + i >= 0 && X + i <= 100 && Y + j >= 0 && Y + j <=100
							&& Math.abs(i) + Math.abs(j) <= 2) {
						searcher.setFirst(X + i);
						searcher.setSecond(Y + j);
					
						if (strategyMap.containsKey(searcher)) {
							pointXY = strategyMap.get(searcher);
							if (strategyList.contains(pointXY)) {
								strategyList.get(strategyList.indexOf(pointXY)).incrementDanger(-chickenNumbers);
								if (Math.abs(i) + Math.abs(j) < 2) {
									strategyList.get(strategyList.indexOf(pointXY)).incrementProfit(-chickenNumbers);
								}
								strategyList.get(strategyList.indexOf(pointXY)).refreshRating();
							}
						}
					}
				}
			}
			/*
			if ((X + 2) <= 100) {
				searcher.setFirst(X + 2);
				searcher.setSecond(Y);
				if(strategyMap.containsKey(searcher)) {
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).incrementDanger(-chickenNumbers);
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).refreshRating();
				}
			}
			if ((X - 2) >= 0) {
				searcher.setFirst(X - 2);
				searcher.setSecond(Y);
				if(strategyMap.containsKey(searcher)) {
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).incrementDanger(-chickenNumbers);
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).refreshRating();
				}
			}
			if ((Y + 2) <= 100) {
				searcher.setFirst(X);
				searcher.setSecond(Y + 2);
				if(strategyMap.containsKey(searcher)) {
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).incrementDanger(-chickenNumbers);
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).refreshRating();
				}
			}
			if ((Y - 2) >= 0) {
				searcher.setFirst(X);
				searcher.setSecond(Y - 2);
				if(strategyMap.containsKey(searcher)) {
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).incrementDanger(-chickenNumbers);
					strategyList.get(strategyList.indexOf(strategyMap.get(searcher))).refreshRating();
				}
			}
			*/
	}
	
	private static void destroyMineAt(int X, int Y, ArrayList<StrategyPoint> strategyList, 
			Pair<Integer, Integer> searcher, HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap) {
		
			StrategyPoint pointXY;
			for (int i = -1; i < 2; i++) {
				for (int j = -1; j < 2; j++) {
					if (X + i >= 0 && X + i <= 100 && Y + j >= 0 && Y + j <=100
							&& Math.abs(i) + Math.abs(j) <= 1) {
						searcher.setFirst(X + i);
						searcher.setSecond(Y + j);
					
						if (strategyMap.containsKey(searcher)) {
							pointXY = strategyMap.get(searcher);
							if (strategyList.contains(pointXY)) {
								strategyList.get(strategyList.indexOf(pointXY)).incrementProfit(-6);
								strategyList.get(strategyList.indexOf(pointXY)).refreshRating();
							}
						}
					}
				}
			}
	}

	private static void fortifyMine(
			HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap,
			Pair<Integer, Integer> searcher, Integer coordinateX,
			Integer coordinateY) {

			StrategyPoint existingPoint;
			for(int i = -1; i < 2; i++) {
				for(int j = -1; j < 2; j++) {
					if (coordinateX + i >= 0 && coordinateY + j >= 0 
							&& coordinateX + i <= 100 && coordinateY + j <= 100
							&&  Math.abs(i) + Math.abs(j) <= 1) {
						searcher.setFirst(coordinateX + i);
						searcher.setSecond(coordinateY + j);
					
						if (strategyMap.containsKey(searcher)) {
							existingPoint = strategyMap.get(searcher);
							existingPoint.incrementProfit(6);
							existingPoint.setHasMine(true);
							existingPoint.refreshRating();
						} else {
							Pair<Integer, Integer> newStrategyCoordinates = new Pair<Integer, Integer>(coordinateX + i, coordinateY + j);
							StrategyPoint newStrategyPoint = new StrategyPoint(coordinateX + i, coordinateY + j, 6, 0, true);
							strategyMap.put(newStrategyCoordinates, newStrategyPoint);
						}
					}
				}
			}
	}
	
	private static void reduceStrategyPoints(
	//Not used for now!.
			HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap,
			Pair<Integer, Integer> searcher,
			ArrayList<StrategyPoint> strategyList, Boolean[] flags) {
		
		for(int i = 0; i < strategyList.size(); i++) {
			if (flags[i]) {
				int positionX = strategyList.get(i).getX();
				int positionY = strategyList.get(i).getY();
				for(int a = -2; a < 3; a++) {
					for (int b = -2; b < 3; b++) {
						if(positionX + a >= 0 && positionX + a <= 100 && 
								positionY + b >= 0 && positionY + b <=100 && 
								!(a == 0 && b == 0)) {
							
							searcher.setFirst(positionX + a);
							searcher.setSecond(positionY + b);
							
							
							StrategyPoint pnt = strategyMap.get(searcher);
							if(strategyList.contains(pnt)) {
								int index = strategyList.indexOf(pnt);
								flags[index] = false;
							}
						}
					}
				}
			}
		}
	}
	
	private static void sortStrategyMap(HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap, ArrayList<StrategyPoint> strategyList) {		
		for (Map.Entry<Pair<Integer, Integer>, StrategyPoint> entry : strategyMap.entrySet()) {
			strategyList.add(entry.getValue());
		}
		Collections.sort(strategyList, new StrategyPointComparator());
	}
	
	private static int isPayable(int remainingMoney, int requiredMoney) {
		if (requiredMoney <= remainingMoney) {
			return requiredMoney;
		} else {
			return 0;
		}
	}

	private static void fortifyChicken(
			HashMap<Pair<Integer, Integer>, StrategyPoint> strategyMap,
			Pair<Integer, Integer> searcher, Integer chickenNumber,
			Integer coordinateX, Integer coordinateY) {
		
		    StrategyPoint currentPoint;
			for(int i = -2; i < 3; i++) {
				for(int j = -2; j < 3; j++) {
					if (coordinateX + i >= 0 && coordinateY + j >= 0 
							&& coordinateX + i <= 100 && coordinateY + j <= 100
							&& Math.abs(i) + Math.abs(j) <= 2) {
						searcher.setFirst(coordinateX + i);
						searcher.setSecond(coordinateY + j);
				
						if (strategyMap.containsKey(searcher)) {
							currentPoint = strategyMap.get(searcher);
							currentPoint.incrementDanger(chickenNumber);
							if (Math.abs(i) + Math.abs(j) < 2) {
								currentPoint.incrementProfit(chickenNumber);
							}
							currentPoint.refreshRating();
						} else {
							Pair<Integer, Integer> newStrategyCoordinates = new Pair<Integer, Integer>(coordinateX + i, coordinateY + j);
							if (Math.abs(i) + Math.abs(j) < 2) {
								StrategyPoint newStrategyPoint = new StrategyPoint(coordinateX + i, coordinateY + j, chickenNumber, chickenNumber, false);
								strategyMap.put(newStrategyCoordinates, newStrategyPoint);
							} else {
								StrategyPoint newStrategyPoint = new StrategyPoint(coordinateX + i, coordinateY + j, 0, chickenNumber, false);
								strategyMap.put(newStrategyCoordinates, newStrategyPoint);
							}
						}
					}
				}
			}
			/*
			//Update most left
			if ((coordinateX + 2) <= 100) {
				searcher.setFirst(coordinateX + 2);
				searcher.setSecond(coordinateY);
				
				if (strategyMap.containsKey(searcher)) {
					strategyMap.get(searcher).incrementDanger(chickenNumber);
					strategyMap.get(searcher).refreshRating();
				} else {
					Pair<Integer, Integer> newStrategyCoordinates1 = new Pair<Integer, Integer>(coordinateX + 2, coordinateY);
					StrategyPoint newStrategyPoint1 = new StrategyPoint(coordinateX + 2, coordinateY, 0, chickenNumber, false);
					strategyMap.put(newStrategyCoordinates1, newStrategyPoint1);
				}
			}
			//Update most right
			if ((coordinateX - 2) >= 0) {
				searcher.setFirst(coordinateX - 2);
				searcher.setSecond(coordinateY);
				
				if (strategyMap.containsKey(searcher)) {
					strategyMap.get(searcher).incrementDanger(chickenNumber);
					strategyMap.get(searcher).refreshRating();
				} else {
					Pair<Integer, Integer> newStrategyCoordinates2 = new Pair<Integer, Integer>(coordinateX - 2, coordinateY);
					StrategyPoint newStrategyPoint2 = new StrategyPoint(coordinateX - 2, coordinateY, 0, chickenNumber, false);
					strategyMap.put(newStrategyCoordinates2, newStrategyPoint2);
				}
			}
			//Update most top
			if ((coordinateY + 2) <= 100) {
				searcher.setFirst(coordinateX);
				searcher.setSecond(coordinateY + 2);
				
				if (strategyMap.containsKey(searcher)) {
					strategyMap.get(searcher).incrementDanger(chickenNumber);
					strategyMap.get(searcher).refreshRating();
				} else {
					Pair<Integer, Integer> newStrategyCoordinates3 = new Pair<Integer, Integer>(coordinateX, coordinateY + 2);
					StrategyPoint newStrategyPoint3 = new StrategyPoint(coordinateX, coordinateY + 2, 0, chickenNumber, false);
					strategyMap.put(newStrategyCoordinates3, newStrategyPoint3);
				}
			}
			//Update most bottom
			if ((coordinateY - 2) >= 0) {
				searcher.setFirst(coordinateX);
				searcher.setSecond(coordinateY - 2);
				
				if (strategyMap.containsKey(searcher)) {
					strategyMap.get(searcher).incrementDanger(chickenNumber);
					strategyMap.get(searcher).refreshRating();
				} else {
					Pair<Integer, Integer> newStrategyCoordinates4 = new Pair<Integer, Integer>(coordinateX, coordinateY - 2);
					StrategyPoint newStrategyPoint4 = new StrategyPoint(coordinateX, coordinateY - 2, 0, chickenNumber, false);
					strategyMap.put(newStrategyCoordinates4, newStrategyPoint4);
				}
			}*/
	}
}
